package org.iabako.client.ui.form.user.enterprise;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.config.page.PageTagItemEnum;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.InputText;
import org.iabako.client.ui.layout.template.MenuMyAccount;
import org.iabako.client.ui.layout.template.Popup;
import org.iabako.client.ui.layout.template.PopupTagItem;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.entity.user.EnterpriseCustomInfo;
import org.iabako.shared.iservice.user.SignUpServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.List;

/**
 * Created by jose on 02/03/15.
 */
public abstract class MyEnterpriseForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, MyEnterpriseForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final SignUpServiceAsync signUpServiceAsync = SignUpServiceAsync.Util.getInstance();

    @UiField
    public MyEnterpriseConfigForm myEnterpriseConfigForm;
    @UiField
    MyEnterpriseCustomInfoForm myEnterpriseCustomInfoForm;

    public MyEnterpriseForm(PageConfigEnum pageConfigEnum) {
        setForm(binderForm, pageConfigEnum, "");
        Popup.resizeToSmallPopup();

        myEnterpriseCustomInfoForm.setMyEnterpriseConfigForm(myEnterpriseConfigForm);
        myEnterpriseCustomInfoForm.setParentForm(this);

        for (final PopupTagItem popupTagItem : popup.popupTagItems.keySet()) {
            switch (popupTagItem.pageTagItemEnum) {
                case MyEnterpriseConfigForm:
                    popup.popupTagItems.put(popupTagItem, myEnterpriseConfigForm);
                    break;
                case MyEnterpriseCustomInfoForm:
                    popup.popupTagItems.put(popupTagItem, myEnterpriseCustomInfoForm);
                    break;
            }
        }
        popup.setSelectedTag(PageTagItemEnum.MyEnterpriseConfigForm);
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return myEnterpriseConfigForm.getMandatoryFields();
    }

    protected void setEnterprise(Enterprise enterprise) {
        myEnterpriseConfigForm.setEnterprise(enterprise);
        myEnterpriseCustomInfoForm.setEnterprise(enterprise);
    }

    @Override
    protected AbstractEntity getEntity() {
        Enterprise enterprise = (Enterprise) myEnterpriseConfigForm.getEntity();
        enterprise.setInvoiceNote(myEnterpriseCustomInfoForm.invoiceNote.getValue());
        enterprise.getCustomInfoList().clear();

        for (InputText customInfoTitle : myEnterpriseCustomInfoForm.customInfoFieldsMap.keySet()) {
            if (!GenericTools.isEmpty(customInfoTitle.getValue())
                    && !GenericTools.isEmpty(myEnterpriseCustomInfoForm.customInfoFieldsMap.get(customInfoTitle).getValue())) {

                EnterpriseCustomInfo customInfo = null;
                if (myEnterpriseCustomInfoForm.customInfoMap.get(customInfoTitle) != null) {
                    customInfo = myEnterpriseCustomInfoForm.customInfoMap.get(customInfoTitle);
                } else {
                    customInfo = new EnterpriseCustomInfo();
                }
                customInfo.setTitle(customInfoTitle.getValue());
                customInfo.setValue(myEnterpriseCustomInfoForm.customInfoFieldsMap.get(customInfoTitle).getValue());
                enterprise.getCustomInfoList().add(customInfo);
            }
        }

        return enterprise;
    }

    @Override
    public boolean validate() {
        if (!uiValidation()) {
            return false;
        }
        Enterprise enterprise = (Enterprise) serverValidation();
        if (enterprise == null) {
            return false;
        }

        signUpServiceAsync.saveEnterprise(enterprise, enterprise.getCustomInfoList(), new IabakoAsyncCallback<Enterprise>() {
            public void onSuccess(Enterprise result) {
                popup.handleClickClose();

                AuthenticateUserProvider.getUser().setEnterprise(result);

                MenuMyAccount menuMyAccount = (MenuMyAccount) GenericTools.getWidget(DOM.getElementById("menu_my_account_"));
                menuMyAccount.refreshUserInformation();

                ValidationMessage validationMessageInPageContent = ValidationMessage.getFromRootPage();
                validationMessageInPageContent.showSuccessMessage("validation_enterprise_form_modify_success");

                super.onSuccess(result);
            }
        });
        return true;
    }
}
